package com.odp.service.impl;

import com.odp.entity.*;
import com.odp.mapper.*;
import com.odp.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PerferGroupMapper perferGroupMapper;

    @Autowired
    private SubscribeMapper subscribeMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ReplyMapper replyMapper;

    @Override
    public Boolean makePost(Post post){
        //新建一个帖子
        post.setUserId(2);//这个从session中取

        //默认
        post.setHot(0);
        post.setLikes(0);
        post.setReplyNum(0);
        post.setCreateTime(new Date());
        post.setUnreadNum(0);
        //****************************
        //用户发帖数+1
        int flag1 = userMapper.addPostNum(2);//session中取
        int flag2 = postMapper.addPost(post);
        if(flag1>0&&flag2>0){
            return true;
        }
        return false;
    }

    //点赞帖子
    @Override
    public Boolean likePost(int postId){
        postMapper.likePost(postId);//帖子点赞数+1

        /*
        如果用户偏好里面有该偏好  该偏好权重+1   若没有 加入偏好组 初始值（偏好权重）为10
        */
        int perferId = postMapper.getPerferIdByPostId(postId);//该帖子的偏好id

        //根据该帖子的偏好id查询perferGroup表，用户没有该偏好则新建表项，有则权重+1
        if(perferGroupMapper.checkPerfer(2,perferId)!=null){
            //用户有此偏好项 偏好权重+1
            int flag1 = perferGroupMapper.addWeights(2, perferId);
            if (flag1==0){
                return false;
            }
        }
        else {
            System.out.println("5");
            //用户没有此偏好，新添此偏好并设置初始权重为10
            int flag2 = perferGroupMapper.addPerfer(2, perferId);
            System.out.println("6");
            if (flag2==0){
                return false;
            }
        }

        return true;
    }

    //不感兴趣
    @Override
    public void unlike(int postId){
        int perferId = postMapper.getPerferIdByPostId(postId);

        if(perferGroupMapper.checkPerfer(2,perferId)!=null){
            //如果存在该偏好  权重 -2
            perferGroupMapper.decreaseWeights(2,perferId);
        }else {
            //如果不存在该偏好 新添表项 权重初始值为0
            perferGroupMapper.addPerferUnlike(2,perferId);
        }
    }

    @Override
    //初始化社区模块的30条帖子
    public List<PostDto> getPosts() {
        //Session里面拿userId
        int userId=2;

        //1.获取用户所有weights大于0的偏好
        List<Integer> perferIds = perferGroupMapper.selectPerfersOverZero(userId);
        System.out.println(perferIds.toString());
        //2.查询post表获得perfer_id为这些偏好里面的帖子 20个  另外10个随机的不重复 10个
        List<Post> posts = postMapper.get30Posts(perferIds);
        ArrayList<PostDto> postDtos = new ArrayList<>();
        for(Post post:posts){
            PostDto postDto = new PostDto();
            postDto.setPostId(post.getPostId());
            postDto.setUserId(post.getUserId());
            postDto.setTitle(post.getTitle());
            postDto.setPerferId(post.getPerferId());
            postDto.setContent(post.getContent());
            postDto.setPicture(post.getPicture());
            postDto.setHot(post.getHot());
            postDto.setLikes(post.getLikes());
            postDto.setReplyNum(post.getReplyNum());
            postDto.setCreateTime(post.getCreateTime());
            postDto.setUnreadNum(post.getUnreadNum());


            postDto.setUserName(userMapper.getUserNameById(post.getUserId()));//用户名
            postDto.setAvatar(userMapper.getAvatarById(post.getUserId()));//用户头像

            Subscribe subscribe = subscribeMapper.checkSubscribe(post.getUserId(),2);//是否关注
            if(subscribe==null){
                //没关注
                postDto.setIsSubscribed(0);
            }
            else {
                //关注
                postDto.setIsSubscribed(1);
            }


            postDtos.add(postDto);

        }
        return postDtos;
    }

    @Override
    //获取帖子的详情(头部)
    public PostDetailHeadDto getPostDetailHead(int postId) {
        PostDetailHeadDto postDetailHeadDto = new PostDetailHeadDto();
        Post post = postMapper.getPostById(postId);
        postDetailHeadDto.setPostId(post.getPostId());
        postDetailHeadDto.setUserId(post.getUserId());

        postDetailHeadDto.setUserName(userMapper.getUserNameById(post.getUserId()));
        postDetailHeadDto.setAvatar(userMapper.getAvatarById(post.getUserId()));

        postDetailHeadDto.setContent(post.getContent());
        postDetailHeadDto.setPicture(post.getPicture());
        postDetailHeadDto.setLikes(post.getLikes());
        postDetailHeadDto.setReplyNum(post.getReplyNum());
        postDetailHeadDto.setCreateTime(post.getCreateTime());
        return postDetailHeadDto;
    }

    @Override
    //获取帖子的详情(content：即楼层和楼层的回复)
    public List<PostDetailContentDto> getPostDetailContent(int postId) {
        ArrayList<PostDetailContentDto> postDetailContent = new ArrayList<>();
        List<Comment> comments = commentMapper.getAllCommentsFromPost(postId);//这个帖子的所有评论

        for (Comment comment:comments){
            PostDetailContentDto postDetailContentDto = new PostDetailContentDto();
            postDetailContentDto.setCommentId(comment.getCommentId());
            postDetailContentDto.setUserId(comment.getUserId());
            postDetailContentDto.setContent(comment.getContent());
            postDetailContentDto.setLikes(comment.getLikes());
            postDetailContentDto.setCreateTime(comment.getCreateTime());

            postDetailContentDto.setAvatar(userMapper.getAvatarById(comment.getUserId()));
            postDetailContentDto.setUserName(userMapper.getUserNameById(comment.getUserId()));

            ArrayList<ReplyDto> replyDtos = new ArrayList<>();

            List<Reply> replies = replyMapper.getRepliesFromComment(comment.getCommentId());
            for(Reply reply:replies){
                ReplyDto replyDto = new ReplyDto();
                replyDto.setReplyId(reply.getReplyId());
                replyDto.setUserId(reply.getUserId());
                replyDto.setContent(reply.getContent());
                replyDto.setLikes(reply.getLikes());
                replyDto.setCreateTime(reply.getCreateTime());
                //回复人姓名
                replyDto.setUserName(userMapper.getUserNameById(reply.getUserId()));
                //回复人头像
                replyDto.setAvatar(userMapper.getAvatarById(reply.getUserId()));
                replyDtos.add(replyDto);
            }

            postDetailContentDto.setReplies(replyDtos);

            postDetailContent.add(postDetailContentDto);
        }
        return postDetailContent;
    }


}
